package org.whh.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.whh.client.AuthClient;
import org.whh.domain.Employee;
import org.whh.domain.LoginUser;
import org.whh.domain.Tenant;
import org.whh.exception.ErrorCode;
import org.whh.exception.WhhAssert;
import org.whh.mapper.EmployeeMapper;
import org.whh.mapper.TenantMapper;
import org.whh.query.TenantQuery;
import org.whh.service.ITenantService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.whh.util.AjaxResult;
import org.whh.util.PageList;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author whh
 * @since 2023-04-24
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {
    @Autowired
    private TenantMapper tenantMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private AuthClient authClient;
    @Override
    public PageList selectTablesPage(TenantQuery query) {
        Page<Tenant> page = new Page<Tenant>(query.getPage(),query.getRows());
        List<Tenant> tenantList=tenantMapper.loadTablesPage(page,query);
        return new PageList(page.getTotal(),tenantList);
    }

    @Override
    public AjaxResult settlement(Tenant tenant) {
//        校验
        Employee admin = tenant.getAdmin();
        if (!admin.getPassword().equals(admin.getConfirmPassword())) {
            WhhAssert.isEqual(admin.getPassword(), admin.getConfirmPassword(),ErrorCode.CODE_400_TENANT_ADMIN_PASSWORD_NOT_EQ );
//            throw new RuntimeException("两次输入密码不一致,请重新输入");
        }

        List<Tenant> tenants=tenantMapper.selectList(new EntityWrapper<Tenant>()
        .eq("company_name",tenant.getCompanyName()));
        WhhAssert.isNull(tenants, ErrorCode.CODE_400_TENANT_EXSIT);
//        if(tenants!=null||tenants.size()>0){
//            throw new RuntimeException("店铺已经存在,请重新输入");
//        }

        String username=admin.getUsername();
        List<Employee> employees=employeeMapper.selectList(new EntityWrapper<Employee>()
                .eq("username",username));
        WhhAssert.isNull(employees,ErrorCode.CODE_400_TENANT_ADMIN_EXSIT);
//        if(employees!=null||employees.size()>0){
//            throw new RuntimeException("管理员已经存在,请直接登录");
//        }

//        百度审核
        boolean auditResult=true;
        String message="审核通过";
//        远程调用保存loginuser
        LoginUser loginUser=new LoginUser();
        loginUser.setUsername(admin.getUsername());
        loginUser.setPassword(admin.getPassword());
        loginUser.setType(0);
        AjaxResult ajaxResult = authClient.addOrUpdate(loginUser);
        if(!ajaxResult.isSuccess()){
            return ajaxResult;
        }
        Long loginId=Long.valueOf(ajaxResult.getResultObj().toString());
//        保存租户 admin_id
        admin.setRealName(admin.getUsername());
        admin.setInputTime(new Date());
        admin.setState(0);
        admin.setType(1);
        admin.setLoginId(loginId);
        employeeMapper.insert(admin);
//        租户和套餐中间表
        tenant.setAdmin(admin);
        tenant.setRegisterTime(new Date());
        tenant.setState(auditResult?1:2);
        tenant.setAdminId(admin.getId());
        tenantMapper.insert(tenant);

        tenantMapper.saveTenantAndMeal(tenant.getId(),tenant.getMealId());
//        添加审核日志
//        发邮件
        return AjaxResult.me();
    }
}
